home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 242_01 / a63.c < prev    next >
Text File  |  1989-01-11  |  13KB  |  500 lines

  1. /*
  2.     HEADER:        CUG242;
  3.     TITLE:        63701 Cross-Assembler (Portable);
  4.     FILENAME:    A63.C;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.     SEE-ALSO:    A63.H;
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.               63701 Cross-Assembler in Portable C
  13.  
  14.         Copyright (c) 1985,1987 William C. Colley, III
  15.  
  16. Revision History:
  17.  
  18. Ver    Date        Description
  19.  
  20. 0.0    AUG 1987    Derived from version 3.4 of my portable 6800/6801
  21.             cross-assembler.  WCC3.
  22.  
  23. 0.1    AUG 1988    Fixed a bug in the command line parser that puts it
  24.             into a VERY long loop if the user types a command line
  25.             like "A63 FILE.ASM -L".  WCC3 per Alex Cameron.
  26.  
  27. This file contains the main program and line assembly routines for the
  28. assembler.  The main program parses the command line, feeds the source lines
  29. to the line assembly routine, and sends the results to the listing and object
  30. file output routines.  It also coordinates the activities of everything.  The
  31. line assembly routines uses the expression analyzer and the lexical analyzer
  32. to parse the source line and convert it into the object bytes that it
  33. represents.
  34. */
  35.  
  36. /*  Get global goodies:  */
  37.  
  38. #include "a63.h"
  39.  
  40. /*  Define global mailboxes for all modules:                */
  41.  
  42. char errcode, line[MAXLINE + 1], title[MAXLINE];
  43. int pass = 0;
  44. int eject, filesp, forwd, listhex;
  45. unsigned  address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
  46. FILE *filestk[FILES], *source;
  47. TOKEN token;
  48.  
  49. /*  Mainline routine.  This routine parses the command line, sets up    */
  50. /*  the assembler at the beginning of each pass, feeds the source text    */
  51. /*  to the line assembler, feeds the result to the listing and hex file    */
  52. /*  drivers, and cleans everything up at the end of the run.        */
  53.  
  54. static int done, ifsp, off;
  55.  
  56. void main(argc,argv)
  57. int argc;
  58. char **argv;
  59. {
  60.     SCRATCH unsigned *o;
  61.     int newline();
  62.     void asm_line();
  63.     void lclose(), lopen(), lputs();
  64.     void hclose(), hopen(), hputc();
  65.     void error(), fatal_error(), warning();
  66.  
  67.     printf("63701 Cross-Assembler (Portable) Ver 0.1\n");
  68.     printf("Copyright (c) 1985,1987 William C. Colley, III\n\n");
  69.  
  70.     while (--argc > 0) {
  71.     if (**++argv == '-') {
  72.         switch (toupper(*++*argv)) {
  73.         case 'L':   if (!*++*argv) {
  74.                 if (!--argc) { warning(NOLST);  break; }
  75.                 else ++argv;
  76.                 }
  77.                 lopen(*argv);
  78.                 break;
  79.  
  80.         case 'O':   if (!*++*argv) {
  81.                 if (!--argc) { warning(NOHEX);  break; }
  82.                 else ++argv;
  83.                 }
  84.                 hopen(*argv);
  85.                 break;
  86.  
  87.         default:    warning(BADOPT);
  88.         }
  89.     }
  90.     else if (filestk[0]) warning(TWOASM);
  91.     else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
  92.     }
  93.     if (!filestk[0]) fatal_error(NOASM);
  94.  
  95.     while (++pass < 3) {
  96.     fseek(source = filestk[0],0L,0);  done = off = FALSE;
  97.     errors = filesp = ifsp = pagelen = pc = 0;  title[0] = '\0';
  98.     while (!done) {
  99.         errcode = ' ';
  100.         if (newline()) {
  101.         error('*');
  102.         strcpy(line,"\tEND\n");
  103.         done = eject = TRUE;  listhex = FALSE;
  104.         bytes = 0;
  105.         }
  106.         else asm_line();
  107.         pc = word(pc + bytes);
  108.         if (pass == 2) {
  109.         lputs();
  110.         for (o = obj; bytes--; hputc(*o++));
  111.         }
  112.     }
  113.     }
  114.  
  115.     fclose(filestk[0]);  lclose();  hclose();
  116.  
  117.     if (errors) printf("%d Error(s)\n",errors);
  118.     else printf("No Errors\n");
  119.  
  120.     exit(errors);
  121. }
  122.  
  123. /*  Line assembly routine.  This routine gets expressions and tokens    */
  124. /*  from the source file using the expression evaluator and lexical    */
  125. /*  analyzer, respectively.  It fills a buffer with the machine code    */
  126. /*  bytes and returns nothing.                        */
  127.  
  128. static char label[MAXLINE];
  129. static int ifstack[IFDEPTH] = { ON };
  130.  
  131. static OPCODE *opcod;
  132.  
  133. void asm_line()
  134. {
  135.     SCRATCH int i;
  136.     int isalph(), popc();
  137.     OPCODE *find_code(), *find_operator();
  138.     void do_label(), flush(), normal_op(), pseudo_op();
  139.     void error(), pops(), pushc(), trash();
  140.  
  141.     address = pc;  bytes = 0;  eject = forwd = listhex = FALSE;
  142.     for (i = 0; i < BIGINST; obj[i++] = NOP);
  143.  
  144.     label[0] = '\0';
  145.     if ((i = popc()) != ' ' && i != '\n') {
  146.     if (isalph(i)) {
  147.         pushc(i);  pops(label);
  148.         if (find_operator(label)) { label[0] = '\0';  error('L'); }
  149.     }
  150.     else {
  151.         error('L');
  152.         while ((i = popc()) != ' ' && i != '\n');
  153.     }
  154.     }
  155.  
  156.     trash(); opcod = NULL;
  157.     if ((i = popc()) != '\n') {
  158.     if (!isalph(i)) error('S');
  159.     else {
  160.         pushc(i);  pops(token.sval);
  161.         if (!(opcod = find_code(token.sval))) error('O');
  162.     }
  163.     if (!opcod) { listhex = TRUE;  bytes = BIGINST; }
  164.     }
  165.  
  166.     if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
  167.     else if (off) { listhex = FALSE;  flush();  return; }
  168.  
  169.     if (!opcod) { do_label();  flush(); }
  170.     else {
  171.     listhex = TRUE;
  172.     if (opcod -> attr & PSEUDO) pseudo_op();
  173.     else normal_op();
  174.     while ((i = popc()) != '\n') if (i != ' ') error('T');
  175.     }
  176.     source = filestk[filesp];
  177.     return;
  178. }
  179.  
  180. static void flush()
  181. {
  182.     while (popc() != '\n');
  183. }
  184.  
  185. static void do_label()
  186. {
  187.     SCRATCH SYMBOL *l;
  188.     SYMBOL *find_symbol(), *new_symbol();
  189.     void error();
  190.  
  191.     if (label[0]) {
  192.     listhex = TRUE;
  193.     if (pass == 1) {
  194.         if (!((l = new_symbol(label)) -> attr)) {
  195.         l -> attr = FORWD + VAL;
  196.         l -> valu = pc;
  197.         }
  198.     }
  199.     else {
  200.         if (l = find_symbol(label)) {
  201.         l -> attr = VAL;
  202.         if (l -> valu != pc) error('M');
  203.         }
  204.         else error('P');
  205.     }
  206.     }
  207. }
  208.  
  209. #define    NONUM        0
  210. #define    NEEDBYTE    1
  211. #define    HAVENUM        2
  212.  
  213. static void normal_op()
  214. {
  215.     SCRATCH int numctl;
  216.     SCRATCH unsigned attrib, opcode, op1, op2;
  217.     unsigned expr();
  218.     TOKEN *lex();
  219.     void do_label(), error(), unlex();
  220.  
  221.     do_label();
  222.  
  223.     attrib = opcod -> attr;  numctl = NONUM;
  224.     bytes = 1;  opcode = opcod -> valu;  op1 = op2 = 0;
  225.  
  226.     if (attrib & TWO_OP) {
  227.     op1 = expr();  bytes = 3;  numctl = NEEDBYTE;
  228.     if ((token.attr & TYPE) != SEP) { error('S');  return; }
  229.     if (attrib & BIT_OP) {
  230.         if (op1 > 7) { error('V');  return; }
  231.         op1 = 1 << op1;  attrib &= ~BIT_OP;
  232.         if (opcode == 0x71) op1 = ~op1;
  233.     }
  234.     else if (op1 > 0xff) { error('V');  return; }
  235.     }
  236.  
  237.     for (;;) {
  238.     switch (lex() -> attr & TYPE) {
  239.         case REG:    switch (token.valu) {
  240.                 case 'B':    if (opcode < 0x40) ++opcode;
  241.                     else opcode |= opcode < 0x80 ?
  242.                         0x10 : 0x40;
  243.  
  244.                 case 'A':    if (attrib & REGOPT) {
  245.                         attrib = NULL;  numctl = HAVENUM;
  246.                         break;
  247.                         }
  248.                     if (attrib & REGREQ) {
  249.                         attrib &= ~REGREQ;  break;
  250.                     }
  251.                     error('R');  bytes = 3;  return;
  252.  
  253.                 case 'X':    bytes = 3;
  254.                     if (!(attrib & INDEX)) {
  255.                         error('A');  return;
  256.                     }
  257.                     if (numctl == HAVENUM) {
  258.                         if (op2 > 0xff) {
  259.                         error('V');  return;
  260.                         }
  261.                     }
  262.                     else numctl = NEEDBYTE;
  263.                     if (!(attrib & TWO_OP)) --bytes;
  264.                     opcode = (opcode | 0x20) & 0xef;
  265.                     attrib &= REGREQ + TWO_OP;  break;
  266.             }
  267.             if ((lex() -> attr & TYPE) != SEP) unlex();
  268.             break;
  269.  
  270.         case IMM:    bytes = 3;
  271.             if (!(attrib & (IMM16 + IMM8))) {
  272.                 error('A');  return;
  273.             }
  274.             op2 = expr();
  275.             if (attrib & IMM8) {
  276.                 --bytes;
  277.                 if (op2 > 0xff && op2 < 0xff80) {
  278.                 error('V');  return;
  279.                 }
  280.             }
  281.             attrib &= REGREQ;  break;
  282.  
  283.         case OPR:
  284.         case STR:
  285.         case VAL:    unlex();
  286.             op2 = expr();
  287.             if (attrib & REL) {
  288.                 numctl = HAVENUM;  bytes = 2;
  289.                 op2 = word(op2 - (pc + 2));
  290.                 if (op2 > 0x7f && op2 < 0xff80) {
  291.                 error('B');  return;
  292.                 }
  293.                 attrib = NULL;  break;
  294.             }
  295.  
  296.         case SEP:    bytes = 3;
  297.             if (numctl == NEEDBYTE) {
  298.                 if (op2 > 0xff) { error('V');  return; }
  299.                 if (!(attrib & TWO_OP)) --bytes;
  300.                 numctl = HAVENUM;  break;
  301.             }
  302.             if (numctl == HAVENUM || !(attrib & INDEX)) {
  303.                 error('S');  return;
  304.             }
  305.             numctl = HAVENUM;  opcode |= 0x30;
  306.             attrib &= (INDEX + REGREQ + DIROK + TWO_OP);
  307.             break;
  308.  
  309.         case EOL:    if (attrib & ~(DIROK + INDEX + TWO_OP)) {
  310.                 error(attrib & REGREQ || (attrib & (REGOPT+INDEX)
  311.                 == (REGOPT+INDEX)) ? 'R' : 'S');
  312.                 bytes = 3;  return;
  313.             }
  314.  
  315.             if (attrib & TWO_OP) {
  316.                 obj[1] = low(op1);  obj[2] = low(op2);
  317.             }
  318.             else {
  319.                 if (attrib & DIROK && !forwd &